/*
 * 
 * iFinity Software Development - http://www.ifinity.com.au
 * Copyright (c) 2006 - 2009
 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the "Software"), the rights to use, copy, modify, merge, publish, distribute, sublicense
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:

 * 1. The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 *    of the Software.  
 * 2. The software may not be claimed as the original work and may not be sold as a stand alone product

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 */

using System;
using System.Web;

namespace DotNetNuke.Providers.SearchEngineSiteMapProvider
{
    /// <summary>
    /// The GoogleSiteMapHandler is a builtin HTTPHandler for generating a Google Site Map using 
    /// a type of iFinity.Providers.GoogleSiteMap.GoogleSiteMapProvider.
    /// </summary>
    public class SiteMapHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {

            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            response.ClearHeaders();
            response.ClearContent();
            response.ContentType = "text/xml";
            bool debug = false; bool ping = false;
            string debugSetting = GetDebugSetting(request);
            if (debugSetting != null)
            {
                if (debugSetting.ToLower() == "true")
                {
                    debug = true;
                }
                else
                {
                    if (debugSetting == "ping")
                    {
                        debug = true;
                        ping = true;
                    }
                }
            }

            if (ping)
            {
                response.Write("Google Sitemap Generator Ping OK");
            }
            else
            {
                string sitemap = SearchEngineSitemapService.GetGoogleSiteMap(request.Url.ToString(), debug);
                string debugComment = string.Format("<!-- Sitemap Provider Version {0} -->", GetAppVersion());
                string loadExceptionMessage = null;
                if (SearchEngineSitemapService._providerLoadMessages != null && SearchEngineSitemapService._providerLoadMessages.Count > 0)
                {
                    loadExceptionMessage = "\n<!--Exceptions found on Providers Load:";
                    foreach (string message in SearchEngineSitemapService._providerLoadMessages)
                    {
                        loadExceptionMessage += "\n" + message;
                    }
                    loadExceptionMessage += "-->";
                }
                if (sitemap != "")
                {
                    if (debug)
                        sitemap += debugComment;
                    if (loadExceptionMessage != null)
                        sitemap += loadExceptionMessage;
                    response.Write(sitemap);
                }
                else
                {
                    //send back 404 error if there is no sitemap to be generated
                    response.Status = "404 Not Found";
                    response.Write("No entries were generated for the sitemap");
                    if (debug)
                        response.Write(debugComment);
                }
            }
        }
        private string GetDebugSetting(HttpRequest request)
        {
            string result = null;
            if (request["_smpdebug"] != null)
            {
                result = request["_smpdebug"];
            }
            else
            {
                if (request.Params["HTTP__SMPDEBUG"] != null)
                {
                    result = request["HTTP__SMPDEBUG"];
                }
            }
            return result;
        }
        private string GetAppVersion()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetName(false).Version.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
